package com.wondertek.web.controller.sys;

import com.wondertek.common.base.Result;
import com.wondertek.common.log.AutoLog;
import com.wondertek.common.util.security.PreAuthorize;
import com.wondertek.service.dto.sys.req.EmailSendReq;
import com.wondertek.service.dto.sys.req.SysEmailConfigAddReq;
import com.wondertek.service.entity.sys.SysEmailConfigPo;
import com.wondertek.service.service.sys.ISysEmailConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 发送邮件
 */
@RestController
@RequestMapping("/backend/sys/email")
@Tag(name = "邮件配置管理")
public class SysEmailController {

    @Resource
    private ISysEmailConfigService sysEmailConfigService;

    /**
     * 邮件配置
     *
     * @param emailConfigAddReq
     * @return
     */
    @PostMapping("/emailConfig")
    @Operation(summary = "邮件配置")
    @AutoLog(category = "sys", handleType = "log.email.config",
            handleNote = "'邮件配置'")
    @PreAuthorize("@el.check('backend:sys:email:emailConfig')")
    public Result<?> emailConfig(@RequestBody @Validated SysEmailConfigAddReq emailConfigAddReq) {
        sysEmailConfigService.emailConfig(emailConfigAddReq);
        return Result.success();
    }

    /**
     * 获取邮箱配置
     *
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "获取邮箱配置")
    public Result<SysEmailConfigPo> get() {
        SysEmailConfigPo sysEmailConfigPo = sysEmailConfigService.getEmailConfig();
        return Result.success(sysEmailConfigPo);
    }

    /**
     * 发送邮件
     *
     * @param emailSendReq
     * @return
     */
    @PostMapping("/send")
    @Operation(summary = "发送邮件")
    public Result<?> send(@RequestBody @Validated EmailSendReq emailSendReq) {
        sysEmailConfigService.send(emailSendReq);
        return Result.success();
    }

    /**
     * 发送模版邮件
     *
     * @param emailSendReq
     * @return
     */
    @PostMapping("/sendTemplateEmail")
    @Operation(summary = "发送模版邮件")
    public Result<?> sendTemplateEmail(@RequestBody @Validated EmailSendReq emailSendReq) {
        sysEmailConfigService.send(emailSendReq.getTos(), emailSendReq.getContent());
        return Result.success();
    }
}
